package com.example.meituan.activity.man.frament;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;

import com.example.meituan.MainActivity;
import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.man.UpdateShopActivity;
import com.example.meituan.activity.user.GridLayoutManager;
import com.example.meituan.activity.user.adapter.ProductAdapter;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.ShopDao;
import com.example.meituan.databinding.UpdatePwdBinding;
import com.google.android.material.button.MaterialButton;

import java.util.List;

public class ManageHomeFragment extends Fragment implements OnItemClickListener {
    View rootView;
    ShopDao shopDao;
    List<ShopBean> list;
    private ProductAdapter adapter;
    String businessId;
    AdminBean currentUser;

    @Override
    public void onItemClick(ShopBean item) {
        Intent intent = new Intent(getContext(), UpdateShopActivity.class);
        intent.putExtra("shopBean", item); // 使用 putExtra() 方法传递 ShopBean 对象
        startActivity(intent);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        currentUser = AdminDao.getUserInfo(inflater.getContext());
        Bundle args = getArguments();
        if (args != null && args.containsKey("businessId")) {
            businessId = args.getString("businessId");
        }
        else {
            businessId=currentUser.getUserId();
        }

        if (shopDao == null) {
            shopDao = new ShopDao(getActivity());
        }
        rootView = inflater.inflate(R.layout.fragment_manage_home, container, false);
        LinearLayout buttonContainer = rootView.findViewById(R.id.man_button_container);
        String[] shopTypes = getResources().getStringArray(R.array.type_array);
        ImageView img=rootView.findViewById(R.id.manage_my_img);
        TextView id=rootView.findViewById(R.id.manage_my_id);
        BusinessBean businessBean=AdminDao.getBusiness(businessId);
        id.setText("id:"+businessId);
        String path = businessBean.getsImg();
        Bitmap bitmap= BitmapFactory.decodeFile(path);
        img.setImageBitmap(bitmap);
        for (int i = 0; i < shopTypes.length; i++) {
            MaterialButton button = new MaterialButton(inflater.getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            button.setLayoutParams(params);
            button.setText(shopTypes[i]);
            button.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FF0000")));
            button.setTextColor(Color.WHITE);
            button.setTypeface(null, Typeface.BOLD);
            final int index = i;// 因为匿名内部类只能访问final或effectively final变量
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(shopTypes[index]=="宝贝"){
                        list = shopDao.getAllShopsByBusinessId(businessId);
                        androidx.recyclerview.widget.RecyclerView recyclerView=rootView.findViewById(R.id.recyclerView);
                        if (list == null || list.isEmpty()) {
                            Toast.makeText(getContext(), "没有数据显示", Toast.LENGTH_SHORT).show();
                            recyclerView.setAdapter(null);
                        } else {
                            adapter = new ProductAdapter(list, ManageHomeFragment.this); ;
                            recyclerView.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
                            recyclerView.setAdapter(adapter);
                        }

                    }
                    if(shopTypes[index]=="推荐"){
                        list = shopDao.getAllShopsByNum(businessId);
                        androidx.recyclerview.widget.RecyclerView recyclerView=rootView.findViewById(R.id.recyclerView);
                        if (list == null || list.isEmpty()) {
                            Toast.makeText(getContext(), "没有数据显示", Toast.LENGTH_SHORT).show();
                            recyclerView.setAdapter(null);
                        } else {
                            adapter = new ProductAdapter(list, ManageHomeFragment.this);
                            recyclerView.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
                            recyclerView.setAdapter(adapter);
                        }

                    }
                    if(shopTypes[index]=="新品"){
                        list = shopDao.getAllShopsByTime(businessId);
                        androidx.recyclerview.widget.RecyclerView recyclerView=rootView.findViewById(R.id.recyclerView);
                        if (list == null || list.isEmpty()) {
                            Toast.makeText(getContext(), "没有数据显示", Toast.LENGTH_SHORT).show();
                            recyclerView.setAdapter(null);
                        } else {
                            adapter = new ProductAdapter(list, ManageHomeFragment.this);
                            recyclerView.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
                            recyclerView.setAdapter(adapter);
                        }
                    }

                }
            });
            buttonContainer.addView(button);

        }

        list = shopDao.getAllShopsByBusinessId(businessId);
        androidx.recyclerview.widget.RecyclerView recyclerView=rootView.findViewById(R.id.man_recyclerView);
        if (list == null || list.isEmpty()) {
            Toast.makeText(getContext(), "没有数据显示", Toast.LENGTH_SHORT).show();
            recyclerView.setAdapter(null);
        } else {
            adapter = new ProductAdapter(list, ManageHomeFragment.this);
            recyclerView.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
            recyclerView.setAdapter(adapter);
        }

        androidx.appcompat.widget.SearchView searchView = rootView.findViewById(R.id.man_home_shop_listView_search);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                List<ShopBean> list = shopDao.getAllShopsByName(businessId,query);
                if (list == null || list.isEmpty()) {
                    Toast.makeText(getContext(), "没有数据显示", Toast.LENGTH_SHORT).show();
                    recyclerView.setAdapter(null);
                } else {
                    adapter = new ProductAdapter(list, ManageHomeFragment.this);
                    recyclerView.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
                    recyclerView.setAdapter(adapter);
                }
                return false;
            }


            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        return rootView;
    }


}